<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="796px"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-descriptions
      :column="2"
      border
      :labelStyle="{ width: '120px', textAlign: 'right' }"
    >
      <el-descriptions-item label="申请单号">
        {{ info.applyNo }}
      </el-descriptions-item>
      <el-descriptions-item label="审核状态">
        <el-tag v-if="info.apStatus == 2" type="danger">已驳回</el-tag>
        <el-tag v-else-if="info.apStatus == 1" type="success">已通过</el-tag>
        <el-tag v-else type="warning">审核中</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="患者信息">
        <span>{{ info.patientName }}</span>
        <span class="pl-5">{{ info.patientSex === 1 ? "女" : "男" }}</span>
        <span class="pl-5">{{ info.patientAge }} 岁</span>
      </el-descriptions-item>
      <el-descriptions-item label="患者主诉">
        {{ info.patientPrincipal }}
      </el-descriptions-item>
      <el-descriptions-item label="患者舌苔">
        <image-preview
          v-if="info.furImg"
          :src="info.furImg"
          :width="100"
          :height="100"
        />
      </el-descriptions-item>
      <el-descriptions-item label="患者面相">
        <image-preview
          v-if="info.faceImg"
          :src="info.faceImg"
          :width="100"
          :height="100"
        />
      </el-descriptions-item>
      <el-descriptions-item label="疾病诊断">
        {{ info.typesResult }}
      </el-descriptions-item>
      <el-descriptions-item label="医嘱">
        {{ info.remark }}
      </el-descriptions-item>
      <el-descriptions-item label="开单时间">
        {{ info.applyDate }}
      </el-descriptions-item>
      <el-descriptions-item label="开单人申请人">
        {{ info.applyExpertName }}
      </el-descriptions-item>
      <el-descriptions-item label="开单人所属机构">
        {{ info.applyCompanyName }}
      </el-descriptions-item>
      <template v-if="info.apStatus == 1 || info.apStatus == 2">
        <el-descriptions-item label="审核时间">
          {{ info.apDate }}
        </el-descriptions-item>
        <el-descriptions-item label="审核人">
          {{ info.apExpertName }}
          <img
            v-if="info.signImg"
            :src="info.signImg"
            style="width: 100px; height: auto"
          />
        </el-descriptions-item>
      </template>
    </el-descriptions>

    <el-card class="mt-20">
      <div slot="header">
        <span class="fw-7">处方1</span>
      </div>
      <div class="girdBox">
        <div
          v-for="(item, index) in info.serviceDetail"
          :key="index"
          class="printOrder fz-14"
        >
          <div class="relative">
            <ParescriptNum :info="{parescriptOrder: info}" :serviceDetailIndex="index" />
            <div class="absolute" style="top: 0;right: 0;">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-printer"
                @click="print({serviceItems: String(item.sheetType), ...item}, index)"
                >打印预览</el-button
              >
            </div>
          </div>
          <ParescriptType
            :info="{parescriptOrder: info}"
            :prescriptionInfo="{serviceItems: String(item.sheetType), ...item}"
             class="my-5"
          />
          <ParescriptItem
            :info="{parescriptOrder: info}"
            :prescriptionInfo="{serviceItems: String(item.sheetType), ...item}"
            :printConfig="printConfig"
          />
        </div>
      </div>
    </el-card>

    <div slot="footer" class="tc">
      <el-button type="primary" @click="closeModal">确定</el-button>
    </div>

    <div style="display: none">
      <div ref="printOrder">
        <PrintDom
          :info="{orderNo: info.applyNo, checkAgencyName: info.checkAgencyName, parescriptOrder: info}"
          :printConfig="printConfig"
          :prescriptionInfo="prescriptionInfo"
          :serviceDetailIndex="serviceDetailIndex"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getDPOrderDetails } from "@/api/dpReview";
import { Print } from "@/utils/print";
import ImageUpload from "@/components/ImageUpload";
import PrintDom from '@/views/order/components/print.vue';
import ParescriptNum from '@/views/order/components/prescription/num.vue';
import ParescriptType from '@/views/order/components/prescription/type.vue';
import ParescriptItem from '@/views/order/components/prescription/item.vue';

export default {
  components: {
    ImageUpload,
    PrintDom,
    ParescriptNum,
    ParescriptType,
    ParescriptItem
  },
  data() {
    return {
      title: "订单详情",
      open: false,
      info: {
        id: undefined,
        prescriptDetail: [],
      },
      prescriptionInfo: null,
      serviceDetailIndex: 0, // 处方号所在的下标
      printConfig: {},
    };
  },
  methods: {
    openModal(id) {
      this.open = true;
      this.info.id = id || undefined;
      this.info.id && this.getData();
    },
    closeModal() {
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    // 获取详情
    getData() {
      getDPOrderDetails(this.info.id).then((res) => {
        this.info = res.data;
        this.info.prescriptionType = res.data.prescriptionType || '0'; // 后端未返回
      });
    },
    print(item, index) {
      this.prescriptionInfo = item;
      this.serviceDetailIndex = index;
      const printConfig = localStorage.getItem("printConfig");
      this.printConfig = printConfig ? JSON.parse(printConfig) : {};

      this.$nextTick(() => {
        Print(this.$refs.printOrder);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.gird {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  background-color: #f5f5f5;
}
</style>
